package net.xdclass.service.impl;

import net.xdclass.controller.Request.LinkGroupAddRequest;
import net.xdclass.controller.Request.LinkGroupUpdateRequest;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.manager.LinkGroupManager;
import net.xdclass.model.LinkGroupDO;
import net.xdclass.mapper.LinkGroupMapper;
import net.xdclass.model.LoginUser;
import net.xdclass.service.LinkGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.xdclass.vo.LinkGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 二当家小D
 * @since 2025-08-15
 */
@Service
public class LinkGroupServiceImpl implements LinkGroupService {
	@Autowired
	private LinkGroupManager linkGroupManager;

	@Override
	public int addGroup(LinkGroupAddRequest linkGroupAddRequest) {
      String title = linkGroupAddRequest.getTitle();
      LoginUser loginUser = LoginInterceptor.threadLocal.get();
      long accontno = loginUser.getAccountNo();
      LinkGroupDO linkGroupDO =new LinkGroupDO();
      linkGroupDO.setAccountNo(accontno);
      linkGroupDO.setTitle(title);
      return linkGroupManager.addGroup(linkGroupDO);
	}

	@Override
	public int delGroup(Long id) {
		LoginUser loginUser = LoginInterceptor.threadLocal.get();
		long accontno = loginUser.getAccountNo();
		return linkGroupManager.delGroup(id,accontno);
	}

	@Override
	public List getList() {
		LoginUser loginUser = LoginInterceptor.threadLocal.get();
		long accontno = loginUser.getAccountNo();
		List <LinkGroupDO> list = linkGroupManager.getList(accontno);
		List <LinkGroupVO> voList= new ArrayList<LinkGroupVO>();
		for(LinkGroupDO linkGroupDO : list){
			LinkGroupVO linkGroupVO = new LinkGroupVO();
			linkGroupVO.setAccountNo(accontno);
			linkGroupVO.setId(linkGroupDO.getId());
			linkGroupVO.setGmtCreate(linkGroupDO.getGmtCreate());
			linkGroupVO.setGmtModified(linkGroupDO.getGmtModified());
			linkGroupVO.setTitle(linkGroupDO.getTitle());
			voList.add(linkGroupVO);
		}
		return voList;

	}

	@Override
	public LinkGroupVO getDetail(Long id) {
		LoginUser loginUser = LoginInterceptor.threadLocal.get();
		long accontNo = loginUser.getAccountNo();
		LinkGroupDO linkGroupDO= linkGroupManager.getDetail(id,accontNo);
		LinkGroupVO linkGroupVO = new LinkGroupVO();
		BeanUtils.copyProperties(linkGroupDO, linkGroupVO);
		return linkGroupVO;
	}

	@Override
	public int update(LinkGroupUpdateRequest linkGroupUpdateRequest) {
		LoginUser loginUser = LoginInterceptor.threadLocal.get();
		long accontno = loginUser.getAccountNo();
		long id = linkGroupUpdateRequest.getId();
		String title = linkGroupUpdateRequest.getTitle();
		LinkGroupDO linkGroupDO = new LinkGroupDO();
		linkGroupDO.setId(id);
		linkGroupDO.setAccountNo(accontno);
		linkGroupDO.setTitle(title);
		int rows = linkGroupManager.update(linkGroupDO);
		return rows;
	}

}
